package com.jingdianjichi.subject.application.controller;

import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConvert;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConvert;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.enums.IsDeleteFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConvert;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConvert;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 刷题分类
 */
@Slf4j
@RequestMapping("/subject/category")
@RestController
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;
    private SubjectCategoryDTOConvert subjectCategoryDTOConvert;

    private SubjectLabelConvert subjectLabelConvert;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryDTO  dto的日志:{}", subjectCategoryDTO);
            }
            //guava 参数校验 对参数进行规则 自定义报错信息
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类参数不能为空");//类型id不为空
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()),"分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类父亲级别不能为空");
            SubjectCategoryBO subjectCategory=SubjectCategoryDTOConvert.INSTANCE.convertBoToCategory(subjectCategoryDTO);
            subjectCategory.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
            subjectCategoryDomainService.add(subjectCategory);
            return Result.ok(true);
        }catch (Exception e) {
            log.error("SubjectCategoryController.add error:{}",e.getMessage());
            return Result.fail();
        }

    }

    /**
     * 查询分大类
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryDTO  dto的日志:{}", subjectCategoryDTO);
            }
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList= subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList=SubjectCategoryDTOConvert.INSTANCE.convertBoToCategoryDTOList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory error:{}",e.getMessage());
            return Result.fail("查询失败");
        }

        }

    /**
     * 大类下的分类查询
     * @param subjectCategoryDTO
     * @return
     */

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
            try {
                if(log.isInfoEnabled()){
                    log.info("SubjectCategoryDTO  dto的日志:{}", subjectCategoryDTO);
                }
                SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertBoToCategory(subjectCategoryDTO);
                subjectCategoryBO.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());

                Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类ID不能为空");
                List<SubjectCategoryBO> subjectCategoryBOList= subjectCategoryDomainService.queryCategory(subjectCategoryBO);
                List<SubjectCategoryDTO> subjectCategoryDTOList=SubjectCategoryDTOConvert.INSTANCE.convertBoToCategoryDTOList(subjectCategoryBOList);
                return Result.ok(subjectCategoryDTOList);
            }catch (Exception e) {
                log.error("SubjectCategoryController.queryPrimaryCategory error 错误日志:{}",e.getMessage());
                return Result.fail("查询失败");
            }

        }

    /**
     * 更新分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
                try {
                    if(log.isInfoEnabled()){
                        log.info("SubjectCategoryDTO :{}", subjectCategoryDTO);
                    }
                    Preconditions.checkNotNull(subjectCategoryDTO.getId());
                    SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertBoToCategory(subjectCategoryDTO);
                    Boolean result=subjectCategoryDomainService.update(subjectCategoryBO);
                    return Result.ok(result);

                }catch (Exception e) {
                    log.error("SubjectCategoryController.update error:{}",e.getMessage());
                    return Result.fail("更新失败");
                }

        }

    /**
     * 分类删除
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
                try {
                    if(log.isInfoEnabled()){
                        log.info("SubjectCategoryDTO :{}", subjectCategoryDTO);
                    }
                    Preconditions.checkNotNull(subjectCategoryDTO.getId());
                    SubjectCategoryBO subjectCategoryBO=SubjectCategoryDTOConvert.INSTANCE.convertBoToCategory(subjectCategoryDTO);
                    Boolean result =subjectCategoryDomainService.delete(subjectCategoryBO);
                    return Result.ok(result);
                }catch (Exception e) {
                    log.error("SubjectCategoryController.delete error:{}",e.getMessage());
                    return Result.fail("删除失败！");
                }



        }

    /**
     * 查询分类及标签 一次性
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryDTO  dto的日志:{}", subjectCategoryDTO);
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertCategoryDTOToBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList= subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBO);
            List<SubjectCategoryDTO> categoryDTOList=SubjectCategoryDTOConvert.INSTANCE.convertBoToCategoryDTOList(subjectCategoryBOList);
            List<SubjectCategoryDTO> labelDTOS=new LinkedList<>();
            subjectCategoryBOList.forEach(bo -> {
                SubjectCategoryDTO categoryDTO = subjectCategoryDTOConvert.INSTANCE.convertBoToSubjectCategoryDTO(bo);
                List<SubjectLabelDTO> labelDTOList = SubjectLabelDTOConvert.INSTANCE.convertBoToCategoryDTOList(bo.getLabelBOList());
                categoryDTO.setLabelDTOList(labelDTOList);
                labelDTOS.add(categoryDTO);
            });
            return Result.ok(labelDTOS);
        }catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory error:{}",e.getMessage());
            return Result.fail("查询失败");
        }

    }
}
